//
//  main.c
//  C 面向指针编程
//
//  Created by Robin on 16/4/4.
//  Copyright © 2016年 cnbm. All rights reserved.
//

#include <stdio.h>
#include <stdlib.h>

// 模拟一个链条

//链条的一节
struct node {
    
    struct node *pre;
    struct node *next;
    void *shape;
};

struct point {
    double x;
    double y;
};

struct rectAngle{
    double width;
    double height;
};

struct circle {
    struct point *center;
    double radius;
};

struct chain_node_shape {
    struct rectAngle *body;
    struct circle *holes[2];
};

struct chain_node_shape * create_chain_shape (struct circle *c1,struct circle *c2,struct rectAngle *body){
    
    struct chain_node_shape *ret = malloc(sizeof(struct chain_node_shape));
    ret->body = body;
    ret->holes[0] = c1;
    ret->holes[1] = c2;
    return ret;
}

struct rectAngle* create_rect (double w,double h){
    
    struct rectAngle *rect = malloc(sizeof(struct rectAngle));
    rect->height = h;
    rect->width = w;
    return rect;
}

struct point* create_point (double x,double y){
    struct point *p = malloc(sizeof(struct point));
    p->x = x;
    p->y = y;
    return p;
}

struct circle* create_circle (struct point *p,double radius){
    
    struct circle *c1 = malloc(sizeof(struct circle));
    c1->center = p;
    c1->radius = radius;
    
    return c1;
}

struct node* create_chain_node (void){
    
    double radius = 0.5;
    double left_x = 1.0;
    double left_y = 1.0;
    struct point *left_center = create_point(left_x, left_y);
    struct circle *c1 = create_circle(left_center, radius);
    
    double right_x = 9.0;
    double right_y = 1.0;
    struct point *right_center = create_point(right_x, right_y);
    struct circle *c2 = create_circle(right_center, radius);
    
    struct rectAngle *rect = create_rect(10.0, 2.0);
    
    struct chain_node_shape *shape = create_chain_shape(c1, c2, rect);
    
    struct node *node = malloc(sizeof(struct node));
    node->next = NULL;
    node->pre = NULL;
    node->shape = shape;
    
    return node;
}

int main(int argc, const char * argv[]) {

    //
//    struct node a,b,c;
//    
//    a.next = &b;
//    b.pre = &a;
//    
//    b.next = &c;
//    c.pre = &b;
//    
//    c.next = &a;
//    a.pre = &c;
//    
//    // 建造一个1000节的链条
//    struct node *head = malloc(sizeof(struct node));
//    struct node *tail = head;
//    
//    for (int i=0; i<1000; i++) {
//        
//        struct node *new_tail = malloc(sizeof(struct node));
//        tail->next = new_tail;
//        new_tail->pre = tail;
//        
//        tail = new_tail;
//    }
//    
//    tail->next = head;
//    head->pre = tail;
    
    struct node *head = create_chain_node();
    struct node *tail = head;
    
    for (int i=0; i<1000; i++) {
        
        struct node *new_node = create_chain_node();

        tail->next = new_node;
        new_node->pre = tail;
        
        tail = new_node;
    }
    
    tail->next = head;
    head->pre = tail;
    
    
    return 0;
}
